﻿package wibrst.loading {
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import wibrst.utils.EnvUtil;
	import wibrst.utils.VisualUtil;
	
	/**
	 * ...
	 * @author wibrst@qq.com
	 */
	public class LoadHostBase extends EventDispatcher {
		
		public function set nIntervalAvoidSwfCache(value:Number):void {
			_nIntervalAvoidSwfCache = value;
		}
		
		public function setVClsLoading( value:Class, cont:DisplayObjectContainer, pCenterHost:Point ):void {
			_vClsLoading = value;
			_contOfLoading = cont;
			_pCenterHost = pCenterHost;
		}
		
		public function get pCenterHost():Point { return _pCenterHost; }
		
		public function VLoadHost():void {
			_nParallelLoading = 0;
			_nIntervalAvoidSwfCache = 1000;
			_dictLoading = new Object;
		}
		
		/**
		 * for derived class invoke
		 * @param	vcLoadUnits
		 */
		protected function loadContent( vcLoadUnits:Vector.<LoadUnit> ):void {
			
			this._bInLoad = true;
			++_nParallelLoading;
			
			trace( this, "vcLoadUnits", vcLoadUnits );
			
			this._dictLoading = new Object;
			
			var loadingLaunch:VUIURLLoading = (new _vClsLoading) as VUIURLLoading;
			this._dictLoading[loadingLaunch.sIdSwfLoad] = loadingLaunch;
			
			this._contOfLoading.addChild(loadingLaunch);		/// +++ set the loading to top
			 setDisplayCenterPoint( loadingLaunch, _pCenterHost );
			
			 if( _nIntervalAvoidSwfCache > 0 ){	/// swf cache fucker
				var nStampAvoidSwfCache:Number = int( new Date().getTime() / _nIntervalAvoidSwfCache );
				var sURLCacheBlock:String= EnvUtil.checkInBrowser()? "?" + nStampAvoidSwfCache : "" ;
				 for each( var loadUnit:LoadUnit in vcLoadUnits ) {
					 loadUnit.addUrlSuffix( sURLCacheBlock );
				 }
			 }
			
			/// start load
			loadingLaunch.addEventListener(VKernelLoading.LOAD_ALL_COMP, loadingCompHandler);
			loadingLaunch.startLoading( vcLoadUnits );
		}
		
		private function setDisplayCenterPoint( disp:DisplayObject, pCenter:Point = null ):void {
			if ( ! pCenter ) pCenter = new Point;
			var rectBounds:Rectangle = disp.getBounds( disp );
			//trace( "setDisplayCenterPoint", disp, rectBounds, pCenter );
			disp.x = pCenter.x - (rectBounds.x + rectBounds.width / 2);
			disp.y = pCenter.y - (rectBounds.y + rectBounds.height / 2);
		}
		
		private function loadingCompHandler(e:Event):void {
			var loadingComp:VUIURLLoading = e.currentTarget as VUIURLLoading;
			
			loadingComp.removeEventListener(VKernelLoading.LOAD_ALL_COMP, loadingCompHandler);
			
			//trace( this,  "loadingCompHandler--------------------------" );
			trace( this, "---", loadingComp.getUldr(0) );
			
			if ( --_nParallelLoading == 0 ) {
				_bInLoad = false;
				trace( this, "all parallel loading done!" );
			}
			
			/// get kernel content
			loadThreadCompHandler( loadingComp );
			
			/// destroy this loading
			autoRemoveChild( loadingComp );
			
			_dictLoading[loadingComp.sIdSwfLoad] = null;
			loadingComp = null;
		}
		
		private function autoRemoveChild( disp:DisplayObject ):void {
			if ( disp){
				if ( disp.parent ) disp.parent.removeChild( disp );
				if ( disp is MovieClip ) {
					( disp as MovieClip ).gotoAndStop( 1 );
				}
			}
		}
		
		/**
		 * for override to handle while the loaded content all complete
		 */
		protected function loadThreadCompHandler( loading:VUIURLLoading ):void{}
		
		/// fuck swf cache
		private var _nIntervalAvoidSwfCache:Number;
		
		/// visions
		private var _vClsLoading:Class;
		private var _contOfLoading:DisplayObjectContainer;
		
		private var _dictLoading:Object;
		
		/// control variables
		private var _bInLoad:Boolean;
		private var _nParallelLoading:Number;
		private var _pCenterHost:Point;
		
	}

}